<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use think\facade\Config;
use think\Request;
use app\model\Project as Model;

class Project extends BaseController
{
    /**
     * 列表查询
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(Request $request)
    {
        $params = $request->param();
        $page   = isset($params['page']) ? $params['page'] : 1;
        $limit  = isset($params['limit']) ? $params['limit'] : 10;
        $where  = [
            ['delete_time', '=', 0]
        ];
        if (isset($params['name']) && !empty($params['name'])) {
            $where[] = ['name', 'like', "%{$params['name']}%"];
        }
        $pageLimit = [
            'page'      => $page,
            'list_rows' => $limit,
        ];
        $paginate  = Model::where($where)
            ->order('id', 'desc')
            ->paginate($pageLimit);
        return $this->pageData($paginate);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 项目添加
     *
     * @param Request $request
     * @return \think\response\Json
     */
    public function save(Request $request)
    {
        $params = $request->param();
        $this->validate($params, [
            'name' => 'require',
        ], [
            'name.require' => '项目名称不能为空',
        ]);
        $data   = [
            'name'        => $params['name'],
            'create_time' => $request->time(),
            'delete_time' => 0,
        ];
        $model  = new Model();
        $result = $model->save($data);
        if (!$result) {
            return $this->error();
        }
        return $this->success();
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        $dataModel = Model::find($id);
        if (!$dataModel || $dataModel->delete_time != 0) {
            return $this->error('项目信息不存在！');
        }
        return $this->repData($dataModel);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 更新项目信息
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $params = $request->param();
        $this->validate($params, [
            'name' => 'require',
        ], [
            'name.require' => '项目名称不能为空！',
        ]);
        $dataModel = Model::find($id);
        if (empty($dataModel) || $dataModel->delete_time != 0) {
            return $this->error('项目信息不存在');
        }
        $data   = [
            'id'          => $id,
            'name'        => $params['name'],
            'update_time' => $request->time(),
        ];
        $result = $dataModel->save($data);
        if (!$result) {
            return $this->error();
        }
        return $this->success();
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $dataModel = Model::find($id);
        if (!$dataModel || $dataModel->delete_time != 0) {
            return $this->error('项目信息不存在');
        }
        $data   = [
            'id'          => $id,
            'delete_time' => time(),
        ];
        $result = $dataModel->save($data);
        if (!$result) {
            return $this->error();
        }
        return $this->success();
    }

    public function upload()
    {
        $file = request()->file('file');
        $extension = $file->extension();
        $fileName = str_replace('.'.$extension,'',$file->getOriginalName()).'_'.date('Y-m-d_His');
        $fileName = $fileName.'.'.$extension;
        $savename = \think\facade\Filesystem::putFileAs( 'project', $file,$fileName);
        $saveDir = Config::get('filesystem.disks.local.url');
        $fileUrl  = request()->domain(). implode('/',array_slice(explode('/',$_SERVER['SCRIPT_NAME']),0,3)).$saveDir.'/'.$savename;
        return $this->success('上传文件成功',['filePath'=>$fileUrl]);
    }
}
